package io.jpress.addon.securityinformation.directive;

import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.JFinal;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.template.Env;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope;
import io.jboot.db.model.Columns;
import io.jboot.web.controller.JbootControllerContext;
import io.jboot.web.directive.annotation.JFinalDirective;
import io.jboot.web.directive.base.JbootDirectiveBase;
import io.jboot.web.directive.base.PaginateDirectiveBase;
import io.jpress.JPressOptions;
import io.jpress.addon.securityinformation.model.JpressAddonSecurityinformationMfdr;
import io.jpress.addon.securityinformation.model.JpressAddonSecurityinformationSrd;
import io.jpress.addon.securityinformation.service.JpressAddonSecurityinformationMfdrService;
import io.jpress.addon.securityinformation.service.JpressAddonSecurityinformationSrdService;
import io.jpress.commons.directive.DirectveKit;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Mfdr 自定义标签
 * anjie 2020年3月26日
 * userid 不可空
 */
@JFinalDirective("SecurityInformationSrdSearchList")
public class SecurityInformationSrdSearchDirective extends JbootDirectiveBase {

    @Inject
    private JpressAddonSecurityinformationSrdService service;

    @Override
    public void onRender(Env env, Scope scope, Writer writer) {

        Controller controller = JbootControllerContext.get();
        int page = controller.getParaToInt(1, 1);
        Integer pageSize = getParaToInt("pageSize", scope,10);
        Long userid = getParaToLong("userid", scope);

        String report_no = controller.getPara("report_no");
        String registration_number = controller.getPara("registration_number");
        Date date_occurrence = controller.getParaToDate("date_occurrence");
        Date date_occurrence1 = controller.getParaToDate("date_occurrence1");
        String name_operator = controller.getPara("name_operator");
        String model = controller.getPara("model");
        String language = getPara("language", scope);
        Columns columns = Columns.create()
                .eq("language", language)
                .eq("user_id",userid)
                .eq("report_no", report_no)
                .eq("registration_number", registration_number)
                .ge("date_submission", date_occurrence).le("date_submission",date_occurrence1)
                .eq("name_operator",name_operator)
                .eq("model",model);

        Page<JpressAddonSecurityinformationSrd> paginate =
                service.paginateByColumns(page, pageSize, columns," date_occurrence desc");

        scope.setLocal("SecurityInformationSrdSearchList",paginate);
        renderBody(env, scope, writer);
    }

    @JFinalDirective("SecurityInformationSrdSearchPaginate")
    public static class TemplatePaginateDirective extends PaginateDirectiveBase {

        @Override
        protected String getUrl(int pageNumber, Env env, Scope scope, Writer writer) {
            HttpServletRequest request = JbootControllerContext.get().getRequest();
            String url = request.getRequestURI();
            String contextPath = JFinal.me().getContextPath();

            boolean firstGotoIndex = getPara("firstGotoIndex", scope, false);

            if (pageNumber == 1 && firstGotoIndex) {
                return contextPath + "/";
            }

            // 如果当前页面是首页的话
            // 需要改变url的值，因为 上一页或下一页是通过当前的url解析出来的
            if (url.equals(contextPath + "/")) {
                url = contextPath + "/securityInformationSrdSearch/index"
                        + JPressOptions.getAppUrlSuffix();
            }
            return DirectveKit.replacePageNumber(url, pageNumber);
        }

        @Override
        protected Page<?> getPage(Env env, Scope scope, Writer writer) {
            return (Page<?>) scope.get("SecurityInformationSrdSearchList");
        }

    }


    @Override
    public boolean hasEnd() {
        return true;
    }
}
